package com.lc.rongzecaifu.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.lc.rongzecaifu.adapter.BorrowingTestAdapter;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.view.SimpleDividerItemDecoration;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;

import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;


public class BorrowManageDetailActivity2 extends BaseActivity implements View.OnClickListener {
    ArrayList<String> mDatas = new ArrayList<String>();

    private PtrClassicDefaultHeader header;
    private PtrFrameLayout store_house_ptr_frame;
    private RecyclerView rv01;
    private LinearLayoutManager mLayoutManager;
    private boolean canLoadMore = true;
    public int nowPage = 1;
    public int totalPages = 1;
    public int pageSize = 0;
    public int totalRows = 0;

    private BorrowingTestAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invest_manage_detail3);
        ButterKnife.bind(this);
        initData();
        initview();
        initRefreshListener();
    }


    public void initview() {
        this.store_house_ptr_frame = (PtrFrameLayout) findViewById(R.id.store_house_ptr_frame);
        this.rv01 = (RecyclerView) findViewById(R.id.rv_01);


//        stringList.add("已回收的投资");

        header = new PtrClassicDefaultHeader(this);
        store_house_ptr_frame.setHeaderView(header);
        mLayoutManager = new LinearLayoutManager(this);
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.line_divider3);
        rv01.addItemDecoration(new SimpleDividerItemDecoration(this, drawable, 30));

        rv01.setLayoutManager(mLayoutManager);
        mAdapter=new BorrowingTestAdapter(this,mDatas);
        rv01.setAdapter(mAdapter);
    }

    protected void initData() {

        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
        }
    }


    private void initRefreshListener() {
     rv01.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                if (dy > 0 && canLoadMore && lastVisibleItemPosition + 1 == mAdapter.getItemCount()) {
                    canLoadMore = false;
                    if (nowPage < totalPages) {
                        // getAccessToken(true, nowPage + 1);
                        UtilToast.show(BorrowManageDetailActivity2.this, "当前页已是最后一页");

                    } else {
                        //UtilToast.show(InvestManageDetailActivity.this, "当前页已是最后一页");
                    }
                }
            }
        });

        store_house_ptr_frame.setPtrHandler(new PtrHandler() {


            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                int firstVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findFirstCompletelyVisibleItemPosition();
                return firstVisibleItemPosition == 0 || mDatas.size() == 0;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mDatas.clear();
                nowPage = 1;
                //    getAccessToken(false, nowPage);
                Toast.makeText(context, "开始刷新", Toast.LENGTH_SHORT).show();

            }
        });
        store_house_ptr_frame.addPtrUIHandler(new PtrUIHandler() {
            @Override
            public void onUIReset(PtrFrameLayout frame) {
                header.onUIReset(frame);
            }

            @Override
            public void onUIRefreshPrepare(PtrFrameLayout frame) {
                header.onUIRefreshPrepare(frame);
            }

            @Override
            public void onUIRefreshBegin(PtrFrameLayout frame) {
                header.onUIRefreshBegin(frame);
            }

            @Override
            public void onUIRefreshComplete(PtrFrameLayout frame) {
                header.onUIRefreshComplete(frame);
            }

            @Override
            public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
                header.onUIPositionChange(frame, isUnderTouch, status, ptrIndicator);
            }
        });
    }

}
